package com.cumt.threeway.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Window;

import com.cumt.threeway.R;
import com.cumt.threeway.util.ThreeWayUtil;

import java.util.Timer;
import java.util.TimerTask;

public class LoadingActivity extends Activity {
	
	private String mIsFirst;
	private static final int MAINMESSAGE_1 = 0;
	private static final int MAINMESSAGE_2 = 1;
	private Timer mTimer = new Timer();
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MAINMESSAGE_1:
				stopTimer();
				goNavigationActivity();
				break;
			case MAINMESSAGE_2:
				stopTimer();
				goMainMangeActivity();
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.activity_loading);
		initData();
	}

	private void initData() {
		mIsFirst = ThreeWayUtil.getInstance().getIsFirstIn(this);
		mTimer.schedule(new TimerTask() {
			@Override
			public void run() {
				if (TextUtils.isEmpty(mIsFirst)) {//判断用户是否第一次启动App
					if (mHandler != null) {
						saveFirstIn();
						mHandler.sendEmptyMessage(MAINMESSAGE_1);
					}
				} else {
					if (mHandler != null) {
						mHandler.sendEmptyMessage(MAINMESSAGE_2);
					}
				}
			}
		}, 1000);
	}
	
	/**
	 * ֹͣ��ʱ����
	 */
	private void stopTimer() {
		if (mTimer != null) {
			mTimer.cancel();
			mTimer = null;
		}
	}
	

	private void goMainMangeActivity() {
		startActivity(new Intent(this,MainManageActivity.class));
		finish();
	}
	
	private void goNavigationActivity() {
		startActivity(new Intent(this, NavigationActivity.class));
		finish();
	}
	
	private void saveFirstIn(){
		ThreeWayUtil.getInstance().setFirstIn(this, "666");
	}
}
